﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using Accessibility;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.VisualBasic;
using MSHTML;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.ExceptionServices;

namespace SUNRPA.CONTROLS.IMPL.util {
    //user32本地系统程序集方法定义
    public class User32Utils {
        
        //钩子委托，如果有控件想添加钩子。可以用此委托，不必再次定义新委托
        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
        public delegate int CallBack(IntPtr hwnd, int lParam);

        //锁定屏幕
        [DllImport("user32.dll")]
        public static extern bool LockWorkStation();

        //锁定鼠标键盘进制输入
        [DllImport("user32.dll")]
        public static extern void BlockInput(bool Block);

        //判断系统是否处于锁屏状态
        [DllImport("GetScreendll.dll", CallingConvention = CallingConvention.Cdecl)]
        public extern static bool IsSessionLocked();

        // 判断窗口是否可见
        /// <summary>
        /// 判断目标窗口是否可见
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <returns>可见true,不可见false</returns>
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        /// <summary>
        /// 创建作业对象
        /// </summary>
        /// <param name="lpJobAttributes">该作业的安全描述符</param>
        /// <param name="name">作业名字</param>
        /// <returns></returns>
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string name);

    
        /// <summary>
        /// 将进程注册为作业对象进程
        /// </summary>
        /// <param name="job">作业对象句柄</param>
        /// <param name="process">进程句柄</param>
        /// <returns></returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);

        /// <summary>
        /// 设置作业对象限制
        /// </summary>
        /// <param name="hJob">标识要限制的作业   </param>
        /// <param name="infoType">枚举类型，用于指明要使用的限制类型</param>
        /// <param name="lpJobObjectInfo">包含限制设置值的数据结构的地址</param>
        /// <param name="cbJobObjectInfoLength">指明第三个参数的大小</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);

        /// <summary>
        /// 获取目标窗口标题的文本长度
        /// </summary>
        /// <param name="hWnd">目标窗口句柄</param>
        /// <returns>标题文本长度</returns>
        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hWnd);

         /// <summary>
        /// 通过窗口句柄获取线程ID(TID)和进程ID(PID)
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="PID">返回进程ID</param>
        /// <returns>返回线程ID</returns>
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int PID);   //获取线程ID

        //装置钩子的函数   
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hInstance, int threadId);
 
        //卸下钩子的函数   
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(IntPtr hookHandler);

        [DllImport("user32.dll")]
        public static extern long GetWindowRect(IntPtr hWnd, ref Rect rect);

        [DllImport("user32.dll")]
        public static extern long GetWindowRect(IntPtr hWnd, out Rectangle rect);
         
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, int nFlags);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        
        //调用鼠标光标位置WindoswsAPI,具体请参见 http://www.office-cn.net/t/api/index.html?setcursorpos.htm
        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int x, int y);
        
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        
        /// <summary>
        /// 最小化窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lpdwProcessId"></param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        static extern int CloseWindow(IntPtr hWnd);

        //左单击
        public static void LeftClick() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftUp, 0, 0, 0, 0);
        }
        //左双击
        public static void LeftDoubleClick() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftUp, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftUp, 0, 0, 0, 0);
        }

        //左下
        public static void LeftDown() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftDown, 0, 0, 0, 0);
        }

        //左上
        public static void LeftUp() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseLeftUp, 0, 0, 0, 0);
        }

        //右单击
        public static void RightClick() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightUp, 0, 0, 0, 0);
        }
        //右双击
        public static void RightDoubleClick() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightUp, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightDown, 0, 0, 0, 0);
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightUp, 0, 0, 0, 0);
        }
        //右下
        public static void RightDown() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightDown, 0, 0, 0, 0);
        }

        //右上
        public static void RightUp() { 
            mouse_event((int)MouseEventEnum.IsAbsolute | (int)MouseEventEnum.MouseRightUp, 0, 0, 0, 0);
        }

        //通过错误码获取错误信息
        [DllImport("Kernel32.dll")]
        public extern static int FormatMessage(int flag, ref IntPtr source, int msgid, int langid, ref string buf, int size, ref IntPtr args);
        /// <summary>
        /// 获取系统错误信息描述
        /// </summary>
        /// <param name="errCode">系统错误码</param>
        /// <returns></returns>
        public static string GetSysErrMsg(int errCode){
            IntPtr tempptr = IntPtr.Zero;
            string msg = null;
            FormatMessage(0x1300, ref tempptr, errCode, 0, ref msg, 1024, ref tempptr);
            return msg;
        }


        /// <summary>
        /// 如果函数执行成功，返回值不为0，如果执行失败，返回值为0
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,           // 窗口的句柄, 当热键按下时，会产生WM_HOTKEY信息，该信息该会发送该窗口句柄
            int id,                      // 定义热键ID,属于唯一标识热键的作用
            uint fsModifiers,            // 热键只有在按下Alt、 Ctrl、Shift、Windows等键时才会生效，即才会产生WM_HOTKEY信息
            uint vk                   // 虚拟键,即按了Alt+Ctrl+ X ，X就是代表虚拟键
            );
  
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                   // 窗口句柄
            int id                         // 要取消热键的ID
            );

        /// <summary>
        /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄，以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。hWnd：设备上下文环境被检索的窗口的句柄
        /// </summary>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetDC(IntPtr hWnd);
 
        /// <summary>
        /// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。
        /// </summary>
        [DllImport("user32.dll")]
        static public extern IntPtr GetDesktopWindow();
 
        /// <summary>
        /// 该函数设置指定窗口的显示状态。
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool ShowWindow(IntPtr hWnd, ShowMessageEnum State);
 
        /// <summary>
        /// 通过发送重绘消息 WM_PAINT 给目标窗体来更新目标窗体客户区的无效区域。
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool UpdateWindow(IntPtr hWnd);
 
        /// <summary>
        /// 该函数将创建指定窗口的线程设置到前台，并且激活该窗口。键盘输入转向该窗口，并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool SetForegroundWindow(IntPtr hWnd);
 
        /// <summary>
        /// 该函数改变一个子窗口，弹出式窗口式顶层窗口的尺寸，位置和Z序。
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);
 
        /// <summary>
        /// 打开剪切板
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool OpenClipboard(IntPtr hWndNewOwner);
 
        /// <summary>
        /// 关闭剪切板
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool CloseClipboard();
 
        /// <summary>
        /// 打开清空
        /// </summary>
        [DllImport("user32.dll")]
        static public extern bool EmptyClipboard();
 
        /// <summary>
        /// 将存放有数据的内存块放入剪切板的资源管理中
        /// </summary>
        [DllImport("user32.dll")]
        static public extern IntPtr SetClipboardData(uint Format, IntPtr hData);

 
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        /// <summary>
        /// 该函数获得一个指定子窗口的父窗口句柄。
        /// </summary>
        public static extern IntPtr GetParent(IntPtr hWnd);
 
        /// <summary>
        /// 该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序，直到窗口程序处理完消息再返回。　
        /// </summary>
        /// <param name="hWnd">其窗口程序将接收消息的窗口的句柄</param>
        /// <param name="msg">指定被发送的消息</param>
        /// <param name="wParam">指定附加的消息指定信息</param>
        /// <param name="lParam">指定附加的消息指定信息</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
 
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
 
        /// <summary>
        /// 该函数将一个消息放入（寄送）到与指定窗口创建的线程相联系消息队列里
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
 
        /// <summary>
        /// 该函数对指定的窗口设置键盘焦点。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        /// <summary>
        /// 该函数改变指定子窗口的父窗口。
        /// </summary>
        [DllImport("user32.dll")]
        public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);
 
        /// <summary>
        /// 获取对话框中子窗口控件的句柄
        /// </summary>
        [DllImport("user32.dll")]
        public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);

        [DllImport("user32.dll")]
		public static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);
        /// <summary>
        /// 该函数向指定的窗体添加一个矩形，然后窗口客户区域的这一部分将被重新绘制。
        /// </summary>
        [DllImport("user32.dll")]
        public extern static int InvalidateRect(IntPtr hWnd, Rectangle rect, int bErase);

        [DllImport("user32.dll")]
        public extern static int InvalidateRect(IntPtr hWnd, IntPtr rect, int bErase);
 
        /// <summary>
        /// 该函数产生对其他线程的控制，如果一个线程没有其他消息在其消息队列里。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern bool WaitMessage();
 
        /// <summary>
        /// 该函数从一个与应用事例相关的可执行文件（EXE文件）中载入指定的光标资源.
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);
 
        /// <summary>
        /// 该函数确定光标的形状。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr SetCursor(IntPtr hCursor);
 
        /// <summary>
        /// 确定当前焦点位于哪个控件上。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr GetFocus();
 
        /// <summary>
        /// 该函数从当前线程中的窗口释放鼠标捕获，并恢复通常的鼠标输入处理。捕获鼠标的窗口接收所有的鼠标输入（无论光标的位置在哪里），除非点击鼠标键时，光标热点在另一个线程的窗口中。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
 
        [DllImport("user32.dll")] 
        public static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
 
        /// <summary>
        /// 该函数检取指定虚拟键的状态。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern ushort GetKeyState(int virtKey);
 
        /// <summary>
        /// 该函数改变指定窗口的位置和尺寸。对于顶层窗口，位置和尺寸是相对于屏幕的左上角的：对于子窗口，位置和尺寸是相对于父窗口客户区的左上角坐标的。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
 
        /// <summary>
        /// 该函数检索指定窗口客户区域或整个屏幕的显示设备上下文环境的句柄，在随后的GDI函数中可以使用该句柄在设备上下文环境中绘图。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, uint flags);
 
        /// <summary>
        /// 用于得到被定义的系统数据或者系统配置信息.
        /// </summary>
        [DllImport("user32.dll")]
        static public extern int GetSystemMetrics(int nIndex);
 
        /// <summary>
        /// 该函数显示或隐藏所指定的滚动条。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int ShowScrollBar(IntPtr hWnd, int bar, int show);
 
        /// <summary>
        /// 该函数可以激活一个或两个滚动条箭头或是使其失效。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int EnableScrollBar(IntPtr hWnd, uint flags, uint arrows);
 
        /// <summary>
        /// 该函数将指定的窗口设置到Z序的顶部。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int BringWindowToTop(IntPtr hWnd);
 
        /// <summary>
        /// 该函数确定给定的窗口句柄是否识别一个已存在的窗口。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int IsWindow(IntPtr hWnd);

        /// <summary>
        /// 该函数将指定的虚拟键码和键盘状态翻译为相应的字符或字符串。该函数使用由给定的键盘布局句柄标识的物理键盘布局和输入语言来翻译代码。
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int ToAscii(int uVirtKey,int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey,int fuState);
        [DllImport("user32.dll")]
        public static extern void AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);

        //窗口闪烁
        [DllImport("user32.dll")]
        public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

 
        /// <summary>
        /// 调用下一个钩子 
        /// </summary>
        /// <param name="idHook"></param>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);
 
        /// <summary>
        /// 获取当前线程ID 
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();
 
        /// <summary>
        /// Gets the main module for the associated process. 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        /// <summary>
        /// get window text
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="StringBuilder">标题</param>
        /// <param name="nMaxCount">最大值</param>
        /// <returns></returns>
        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(IntPtr hWnd,StringBuilder lpString, int nMaxCount);

        [HandleProcessCorruptedStateExceptions]
        public static string GetWindowTitle(IntPtr hWnd) {
            try { 
                StringBuilder sb = new StringBuilder();
                GetWindowText(hWnd, sb, 40960);
                return sb.ToString();
            }catch(System.AccessViolationException e) { 
                return "";
            }
        }

        //获取类的名字
        [DllImport("user32.dll")]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        
        [HandleProcessCorruptedStateExceptions]
        public static string GetWindowClassName(IntPtr hWnd) {
            try { 
                StringBuilder sb = new StringBuilder();
                GetClassName(hWnd, sb, 40960);
                return sb.ToString();
            }catch (System.AccessViolationException e){ 
                return "";
            }
        }

        //根据坐标获取窗口句柄
        [DllImport("user32.dll")]
        public static extern IntPtr WindowFromPoint(Point Point);
        //话焦点矩形
        [DllImport("user32.dll")]
        public static extern long DrawFocusRect(IntPtr hdc, ref Rectangle rect);

        //用指定刷子填充矩形，矩形的右边和底边不会描绘
        [DllImport("user32.dll")]
        public static extern long FillRect(IntPtr hdc, ref Rectangle rect, IntPtr hbrush);

        //根据fuRedraw的设置，重画全部或部分窗口
        [DllImport("user32.dll")]
        public static extern long RedrawWindow(IntPtr hwnd, IntPtr rect,  IntPtr hrgnUpdate, long fuRedraw);
        //根据fuRedraw的设置，重画全部或部分窗口
        [DllImport("user32.dll")]
        public static extern long RedrawWindow(IntPtr hwnd, Rect rect,  IntPtr hrgnUpdate, long fuRedraw);
        //关闭一个内核对象。其中包括文件、文件映射、进程、线程、安全和同步对象等。涉及文件处理时，这个函数通常与vb的close命令相似。应尽可能的使用close，因为它支持vb的差错控制。注意这个函数使用的文件句柄与vb的文件编号是完全不同的
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern long CloseHandle(IntPtr hwnd);

        //获得前台窗口的句柄。这里的“前台窗口”是指前台应用程序的活动窗口
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        /// <summary>
        /// 获取分配给一个字串标识符的消息编号
        /// </summary>
        /// <param name="lpString"></param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint RegisterWindowMessage(string lpString);

        /// <summary>
        /// This function is designed for internal use by Microsoft Active Accessibility and is documented for informational purposes only. Neither clients nor servers should call this function.
        /// </summary>
        /// <param name="lResult">A 32-bit value returned by a previous successful call to the LresultFromObject function.</param>
        /// <param name="refiid">Reference identifier of the interface to be retrieved. This is IID_IAccessible.</param>
        /// <param name="wParam">Value sent by the associated WM_GETOBJECT message in its wParam parameter.</param>
        /// <returns></returns>
        [DllImport("Oleacc.dll", PreserveSig = false)]
        [return: MarshalAs(UnmanagedType.Interface)]
        public static extern object ObjectFromLresult(UIntPtr lResult, [MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam);

        [DllImport("OLEACC.dll", SetLastError = true)]
        public static extern int ObjectFromLresult(int lResult, Guid riid, int wParam, ref IHTMLDocument2? ppvObject);

        [DllImport("Oleacc.dll", PreserveSig = false)]
        public static extern long LresultFromObject (int id, IntPtr wParam, out IntPtr punk);

        /// <summary>
        /// 向窗口发送一条消息。如窗口位于不同的线程中，则利用这个函数可以指定一个超时值，以便在另一个进程挂起的时候防止调用进程也永远挂起
        /// </summary>
        /// 
        /// <param name="hWnd">Long，要接收消息的一个窗口的句柄</param>
        /// <param name="Msg">消息的标识符</param>
        /// <param name="wParam">由消息决定</param>
        /// <param name="lParam">由消息决定</param>
        /// <param name="fuFlags">下述常数的一个或多个</param>
        /// <param name="uTimeout">超时值，采用毫秒为单位</param>
        /// <param name="lpdwResult">用于装载函数结果的一个变量</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);

        //破坏（即清除）指定的窗口以及它的所有子窗口（在vb里使用：用处不大。原文：it is unlikely to be of much use.）
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern long DestroyWindow(IntPtr hwnd);
        
        //Retrieves the address of the specified interface for the object associated with the specified window.
        [DllImport("Oleacc.dll")]
        public static extern int AccessibleObjectFromWindow(IntPtr hwnd, long dwId, byte[] riid, out IAccessible acc);
        
        [DllImport("Oleacc.dll")]
        public static extern int AccessibleObjectFromPoint(POINT ptScreen,out IAccessible acc,out VariantType pvarChild);

        /// <summary>
        /// Retrieves the child ID or IDispatch of each child within an accessible container object.
        /// </summary>
        /// <param name="paccContainer"></param>
        /// <param name="iChildStart"></param>
        /// <param name="cChildren"></param>
        /// <param name="rgvarChildren"></param>
        /// <param name="pcObtained"></param>
        /// <returns></returns>
        [DllImport("Oleacc.dll")]
        public static extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [Out] object[] rgvarChildren, out int pcObtained);
        /// <summary>
        /// 寻找窗口列表中第一个符合指定条件的顶级窗口（在vb里使用：FindWindow最常见的一个用途是获得ThunderRTMain类的隐藏窗口的句柄；该类是所有运行中vb执行程序的一部分。获得句柄后，可用api函数GetWindowText取得这个窗口的名称；该名也是应用程序的标题）
        /// </summary>
        /// <param name="lpClassName">指向包含了窗口类名的空中止（C语言）字串的指针；或设为零，表示接收任何类</param>
        /// <param name="lpWindowName">指向包含了窗口文本（或标签）的空中止（C语言）字串的指针；或设为零，表示接收任何窗口标题</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        public static extern int GetLastActivePopup(int hWnd);
 
        [DllImport("user32.dll")]
        public static extern int AnyPopup();
        [DllImport("user32.dll")]
        public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
        
        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack lpfn, int lParam);

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);

        [DllImport("ole32.dll", EntryPoint = "CreateBindCtx")]
        public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);

        //获取com运行时存储com对象的信息表ROT
        [DllImport("ole32.dll", EntryPoint = "GetRunningObjectTable")]
        public static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot);

        //通过progId获取ClassID
        [DllImport("ole32.dll",CharSet=CharSet.Unicode,PreserveSig=false)]
        public static extern void CLSIDFromProgID([In,MarshalAs(UnmanagedType.LPWStr)] string lpszProgID,[Out]out Guid pclsid);

        //通过Classid获取progid
        [DllImport("ole32.dll",CharSet=CharSet.Unicode,PreserveSig=false)]
        public static extern void ProgIDFromCLSID([In]in Guid pclsid,[Out,MarshalAs(UnmanagedType.LPWStr)] out string lpszProgID);
    
        
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetCurrentThread();
        
        [DllImport("kernel32.dll")]
        public static extern Int32 GetExitCodeThread([In]in IntPtr handle, [Out]out int excode);
        
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int TerminateThread(IntPtr hThread,int dwExitCode);

        [DllImport("kernel32.dll")]
        public static extern void ExitThread([In]in int ptr);

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenThread(ThreadAccess da,bool hireh,int thid);
        
        /// <summary>
        /// 暂停线程。
        /// </summary>
        /// <param name="hThread"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        static extern uint SuspendThread(IntPtr hThread);
        /// <summary>
        /// 恢复线程
        /// </summary>
        /// <param name="hThread"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        static extern int ResumeThread(IntPtr hThread);
    }

    //gdi图形设备接口程序集方法定义
    public class GDIUtils { 
        //获取指定设备场景的边界矩形。每个设备场景都有一个边界矩形，程序员可用它来堆放表示当前图象边界的信息
        [DllImport("gdi32.dll")]
        public static extern long GetBoundsRect(IntPtr hdc, out Rectangle rect, long flags);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
        [DllImport("gdi32.dll")]
        public static extern int DeleteDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        public static extern int DeleteObject(IntPtr hdc);

        /// <summary>
        /// 该方法可以获取设备的硬件信息，可以通过第二个参数nIndex来指定要查询的具体信息。例如我们要用到的以像素为单位的桌面高度DESKTOPVERTRES。
        /// </summary>
        /// <param name="hdc"></param>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

    }
}
